module.exports = app => {
    const mongoose = app.mongoose;
    const Schema = mongoose.Schema;
    const BlockSchema = new Schema({
        title: { // 标题
            type: String,
            required: true
        },
        author: { type: Schema.Types.ObjectId, ref: 'User' }, // 作者
        payment: { type: Schema.Types.ObjectId, ref: 'Payment' }, // 支付方式
        blocks: { // 块
            type: String,
            required: true
        },
        status: { // 1 未发布 2 发布
            type: Number,
            default: 1
        },
        cover: String, // 封面
        publishDate: Date, // 发布时间
    }, { timestamps: { createdAt: 'createDate', updatedAt: 'updateDate' } });

    BlockSchema.pre('findOne', function () {
        this.populate('author', '_id name sex avatarUrl role');
        this.populate({
            path: 'payment',
            // select: 'prescription checkResult.checkResultFlag checkResult.checkResultDetail',
            populate: {
                path: 'creator',
                select: 'openId -role'
            }
        }
    )

    });
    BlockSchema.pre('find', function () {
        this.populate('author', '_id name sex avatarUrl -role');
    });
    BlockSchema.pre('findOneAndUpdate', function () {
        this.populate('author', '_id name sex avatarUrl -role');
        this.populate({
            path: 'payment',
            // select: 'prescription checkResult.checkResultFlag checkResult.checkResultDetail',
            populate: {
                path: 'creator',
                select: 'openId -role'
            }
        })
    });
    
    return mongoose.model('Block', BlockSchema);
};